import random
import os
from fake_useragent import UserAgent

'''
    ip = models.CharField(max_length=30, default='171.88.100.129', verbose_name='访问ip')
    visit_time = models.CharField(max_length=100, verbose_name='访问时间', default='06/Jan/2020:11:55:4')
    request_method = models.CharField(max_length=30, default='GET', verbose_name='请求方法')
    source_url = models.URLField(max_length=300, verbose_name='访问的资源url', default='/')
    protocol = models.CharField(max_length=30, verbose_name='协议', default='HTTP/1.1')
    status = models.IntegerField(default=200, verbose_name='访问状态码')
    
    body_size = models.IntegerField(verbose_name='请求包字节大小', default=0)
    http_refer = models.URLField(default='/', verbose_name='http代理（从站点内哪访问的）')
    user_agent = models.TextField(default='-', verbose_name='UserAgent')
'''


class MyNginxLogClass(object):
    refer_file = 'refer_list.txt'
    body_size_file = ''
    source_file = 'source_url_list.txt'

    example_one_line = '''61.219.11.153 - - [26/Dec/2019:03:19:56 +0800] "\x01\x00\x00\x00" 400 182 "-" "-"'''

    def __init__(self, result_log_file):
        self.result_log_file = result_log_file
        self.fp01 = open(self.result_log_file, 'a+', encoding='utf-8')

    def get_one_ip(self):
        part01 = str(random.randint(40, 223))
        part02 = str(random.randint(10, 196))
        part03 = str(random.randint(10, 240))
        part04 = str(random.randint(10, 230))
        random_ip = part01 + '.' + part02 + '.' + part03 + '.' + part04
        # print(random_ip)
        return random_ip

    def get_one_visit_time(self, day='06', month='Feb', year='2020', hour='07', minute='29', second='44', timezone='+0800'):
        '''
        :param day:
        :param month:
        :param year:
        :param hour:
        :param minute:
        :param second:
        :param timezone:
        :return:
        example: [06/Jan/2020:07:29:44 +0800]
        '''
        result = '''[{day}/{month}/{year}:{hour}:{minute}:{second} {timezone}]'''.format(
            day=day,
            month=month,
            year=year,
            hour=hour,
            minute=minute,
            second=second,
            timezone=timezone,
        )
        return str(result)


    def get_one_request_method(self):
        return 'HTTP/1.1'

    def get_one_source_url(self):
        return

    def get_one_protocol(self):
        pass

    def get_one_status(self):
        pass

    def get_one_body_size(self, status):
        '''
        :param status:
        :return:
        '''
        int_status = int(status)
        if int_status in range(300, 400):
            return str(0)
        elif int_status in range(400, 500):
            return str(random.randint(200, 800))
        else:
            return str(random.randint(500, 10000))

    def get_one_http_refer(self):
        pass

    def get_one_user_agent(self):
        ua = UserAgent()
        # print(ua.random)
        return ua.random

    def generate_one_line(self):
        '''
        生成一条随即日至
        :return:
        '''
        pass




    def make_log(self, line_num):
        '''
        生成随机日志文件
        :param line_num:
        :return:
        '''
        for i in range(0, int(line_num) + 1):
            self.fp01.write()


    def make_spider_log(self, line_num):
        '''
        生成爬虫的日志文件
        :param line_num:
        :return:
        '''
